<?php

/**
 * This is the model class for table "export_information".
 */
class export_information extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'export_information':
	 * @var integer $id
	 * @var string $out_permit
	 * @var string $awb
	 * @var string $vessel
	 * @var string $remark
	 * @var integer $etd
	 * @var integer $mode
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return export_information the static model class
	 */
         public $searchText;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'export_information';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('out_permit, awb, vessel, etd, mode', 'required'),
			array('out_permit, mode', 'length', 'max'=>15),
                        array('out_permit', 'unique', 'allowEmpty' => false, 'caseSensitive' => false),
			array('awb', 'length', 'max'=>25),
			array('vessel', 'length', 'max'=>50),
			array('etd','type','type'=>'date','dateFormat'=>'dd/MM/yyyy'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('searchText', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cargos'=>array(self::MANY_MANY,'cargo','cargo_export(export_information_id,cargo_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
            return array(
                'out_permit' => 'Outward permit',
                'awb' => 'Airway Bill / Bill of loading',
                'vessel' => 'Flight / Vessel',
                'etd' => 'Departure date',
                'mode' => 'Mode of transport',
            );
	}
	
	public function beforeValidate() {
        if(is_numeric($this->etd))
            $this->etd = date('d/m/Y',$this->etd);
        return parent::beforeValidate();
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('out_permit',$this->searchText,true,'or');

		$criteria->compare('awb',$this->searchText,true,'or');

		$criteria->compare('vessel',$this->searchText,true,'or');

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getIdList($id=array()){
		$criteria = new CDbCriteria;
		$criteria->addInCondition('id',$id);
		return new CActiveDataProvider(get_class($this),array(
			'criteria'=>$criteria,
		));
	}
        
	public function beforeSave() {
        if(is_numeric($this->etd))
            return parent::beforeSave();
        $_date = explode("/", $this->etd);
        $this->etd = mktime(0,0,0,$_date[1],$_date[0],$_date[2]);
        return parent::beforeSave();
    }
    
    public function afterSave(){
    	if(!is_numeric($this->etd))
            return parent::afterSave();
        $this->etd = date('d/m/Y',$this->etd);
        return parent::afterSave();
    }
}